package com.zabra.zl.ui.recall;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.OrderListBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.myview.state_layout.OtherView;
import com.zabra.zl.ui.recall.contract.RecallContract;
import com.zabra.zl.ui.recall.presenter.RecallPresenter;
import com.zabra.zl.utils.BaseUtils;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecallActivity extends BaseMvpActivity<RecallPresenter> implements RecallContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.cb_all)
    CheckBox cbAll;
    @BindView(R.id.bt_recall)
    LinearLayout btRecall;
    @BindView(R.id.otherview)
    OtherView otherview;
    private List<OrderListBean.RespDataBean> list;
    private Adp adp;
    private int orderId, comOrderId = 0;
    private Dialog dialog;
    private String strName, strNum, strPrice;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recall;
    }

    @Override
    public void initView() {
        tvTitle.setText("撤单");
        otherview.setHolder(mHolder);
        mPresenter = new RecallPresenter();
        mPresenter.attachView(this);
        orderId = getIntent().getExtras().getInt("orderid");
        mPresenter.getData(1, orderId,"","");


    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(OrderListBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list = bean.getRespData();
            if (list.size() == 0) {
                otherview.showEmptyView();
            } else {
                otherview.showContentView();
                recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
                adp = new Adp(R.layout.item_recall, list);
                recycler.setAdapter(adp);
                adp.openLoadAnimation();
                adp.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        adp.setSelectItem(position);
                        adp.notifyDataSetChanged();
                        comOrderId = list.get(position).getComOrderId();
                        strName = BaseUtils.spliteUtils(list.get(position).getSharesCode(), "-")[0];
                        strNum = list.get(position).getEntrustNum() + "";
                        strPrice = list.get(position).getEntrustPrice() + "";
                    }
                });
            }
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            ToastUtil.showToast(this, bean.getRespDesc());
            finish();
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    private void showDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_recall, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);

        TextView tvName = view.findViewById(R.id.item_name);
        TextView tvNum = view.findViewById(R.id.item_num);
        TextView tvPrice = view.findViewById(R.id.item_price);
        tvName.setText(strName);
        tvNum.setText(strNum);
        tvPrice.setText(strPrice);
        view.findViewById(R.id.iv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.getSubmitData(comOrderId, orderId);
                dialog.dismiss();
            }
        });
    }


    @OnClick({R.id.back, R.id.bt_recall})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bt_recall:
                if (comOrderId == 0) {
                    ToastUtil.showToast(this, "请选择订单");
                } else {
                    showDialog();
                }

                break;
        }
    }

}
